<?php
/*
 * Created by generator
 *
 */

class adherent_model extends Model {
	
	/**
	* Identifiant
	* @var int
	*/
	var $adhidadh;

	/**
	* Nom de l'adhérent
	* @var varchar(255)
	*/
	var $adhlbnom;

	/**
	* Prénom de l'adhérent
	* @var varchar(255)
	*/
	var $adhlbprn;

	/**
	* Adresse complète, avec code postal et ville
	* @var varchar(4000)
	*/
	var $adhtxadr;

	/**
	* Téléphone fixe et/ou mobile
	* @var varchar(50)
	*/
	var $adhlbtel;

	/**
	* Adresse e-mail de l'adhérent
	* @var varchar(255)
	*/
	var $adhlbmai;

	/**
	* Photo de l'adhérent
	* @var blob
	*/
	var $adhlbpho;

	
	/**
	 * Constructeur
	 */
	function adherent_model(){
		parent::Model();
		$this->load->helper('adherent');
		
	}
	
	/************************************************************************
	 * Methodes de mise a jour a partir de la base de donnees
	 ************************************************************************/

	/**
	 * Cree une nouvelle instance a partir d'un enregistrement de base de donnees
	 */
	static function adherent_modelFromRow($row){
		$model = new adherent_model();
		$model->adhidadh = $row['adhidadh'];
		$model->adhlbnom = $row['adhlbnom'];
		$model->adhlbprn = $row['adhlbprn'];
		$model->adhtxadr = $row['adhtxadr'];
		$model->adhlbtel = $row['adhlbtel'];
		$model->adhlbmai = $row['adhlbmai'];
		$model->adhlbpho = $row['adhlbpho'];
		return $model;
	}

	/**
	 * recupere tous les enregistrements
	 * @param $db connexion a la base de donnees
	 */
	static function getAlladherents($db, $orderBy){
		$rows = getAlladherentsFromDB($db, $orderBy);
		$records = array();
		foreach ($rows as $row) {
			$records[] = adherent_model::adherent_modelFromRow($row);
		}
		return $records;
	}
	
	/**
	 * Recupere l'enregistrement a partir de son id
	 * @param $db connexion a la base de donnees
	 * @param $adhidadh identifiant de l'enregistrement a recuperer
	 */
	static function getadherent($db, $adhidadh){
		$row = getadherentRow($db, $adhidadh);
		return adherent_model::adherent_modelFromRow($row);
	}
	
	/**
	 * Suppression d'un enregistrement
	 * @param $db connexion a la base de donnees
	 * @param $adhidadh identifiant de l'enregistrement a supprimer
	 */
	static function delete($db, $adhidadh){
		deleteadherent($db, $adhidadh);
	}

	/**
	 * Enregistre en base un nouvel enregistrement
	 * @param $db connexion a la base de donnees
	 */
	public function save($db){
		$this->adhidadh = insertNewadherent($db, $this->adhlbnom, $this->adhlbprn, $this->adhtxadr, $this->adhlbtel, $this->adhlbmai, $this->adhlbpho);
	}

	/**
	 * Mise a jour des donnees d'un enregistrement
	 * @param $db connexion a la base de donnees
	 */
	public function update($db){
		updateadherent($db, $this->adhidadh, $this->adhlbnom, $this->adhlbprn, $this->adhtxadr, $this->adhlbtel, $this->adhlbmai);
	}

	
	/***************************************************************************
	 * USER DEFINED FUNCTIONS
	 ***************************************************************************/

	/**
	 * Mise a jour de la photo
	 * @param $db connexion a la base de donnees
	 */
	public function updatePhoto($db){
		updatePhotoAdherent($db, $this->adhidadh, $this->adhlbpho);
	}
	
	
	static function createThumbnail($path, $imgName, $thumbnailsPath, $newName, $maxiWidth, $maxHeight){
		// create thumbnail
		$original = imagecreatefromjpeg($path . $imgName);
		list($width, $height, $type, $attr) = getimagesize($path . $imgName);
		if( $width > $height ) {
			// landscape
			$newWidth = $maxiWidth;
			$newHeight = ($height / $width) * $newWidth; 
		}else{
			// portrait
			$newHeight = $maxHeight;
			$newWidth = ($width / $height) * $newHeight;
		}
		$tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image");
    	imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy");
		imagejpeg($tempImg, $thumbnailsPath.'/'.$newName, /*quality*/80) or die("Cant save image");
    	
    	imagedestroy($original);
    	imagedestroy($tempImg);
	}
	
	static function getAllAdherentsForActivite($db, $actidact, $orderBy){
		$rows = getAllAdherentsForActiviteFromDB($db, $actidact, $orderBy);
		$records = array();
		foreach ($rows as $row) {
			$records[] = adherent_model::adherent_modelFromRow($row);
		}
		return $records;
	}
}

?>
